/**
 * FileName: PhotoAdapter.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-10 下午4:53:30
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-10 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.ui.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.me.safebox.R;
import com.me.safebox.ui.core.SafeboxAdapter;
import com.me.safebox.ui.curl.CurlActivity;
import com.me.safebox.ui.photo.SelectAllListener;

/**
 * @ClassName: PhotoAdapter
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-10 下午4:53:30
 * 
 */
public class FileEncryptAdapter extends SafeboxAdapter {

	private Context mContext = null;

	private FinalBitmap mFinalBitmap = null;

	private File[] mFiles = null;
	
	// 当用户点击了全选，我用这个数组去记录单个点击取消的对象
	// 当用户没点击全选，我用这个数组去记录点击选中的对象
	private List<String> tempList = new ArrayList<String>();// sosMap

	private ViewGroup parent = null;

	private boolean selectedAll = false;// 是否全选

	public FileEncryptAdapter(Context context, FinalBitmap finalBitmap) {
		mContext = context;
		mFinalBitmap = finalBitmap;
	}

	public SelectAllListener onSelectAllListener = new SelectAllListener() {

		ImageView selectImgView = null;

		@Override
		public void onSelectAll() {
			selectedAll = true;
			int index = 0;
			// 为当前屏幕的所有元素设置选中状态
			for (index = 0; index < parent.getChildCount(); index++) {
				RelativeLayout layout = (RelativeLayout) parent.getChildAt(index);
				selectImgView = (ImageView) layout.findViewById(R.id.select_imgview);
				selectImgView.setBackgroundResource(R.drawable.select_icon);
				selectImgView.setTag(true);

				// 将File[]中的所有元素放入结果集中
				for (File mFile : mFiles) {
					if (!tempList.contains(mFile.getAbsolutePath())) {
						tempList.add(mFile.getAbsolutePath());
					}
				}
			}
			Toast.makeText(mContext, "全部选中" + tempList.size() + "项", Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onUnSelectAll() {
			selectedAll = false;
			int index = 0;
			// 为当前屏幕的所有元素设置取消选中状态
			for (index = 0; index < parent.getChildCount(); index++) {
				RelativeLayout layout = (RelativeLayout) parent.getChildAt(index);
				selectImgView = (ImageView) layout.findViewById(R.id.select_imgview);
				selectImgView.setBackgroundResource(R.drawable.unselect_icon);
				selectImgView.setTag(false);
			}
			Toast.makeText(mContext, "全部取消:" + tempList.size() + "项", Toast.LENGTH_SHORT).show();
			if (tempList.size() > 0) {
				tempList.clear();
			}

		}

	};

	public void setData(File[] files) {
		mFiles = files;
	}

	@Override
	public int getCount() {
		if (mFiles != null) {
			return mFiles.length;
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		this.parent = parent;
		DataHolder dataHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_photo_item, null);
			dataHolder = new DataHolder();
			//dataHolder.pkgNameView = (TextView) convertView.findViewById(R.id.ablum_text_view);
			dataHolder.imgView = (ImageView) convertView.findViewById(R.id.ablum_first_img_view);
			dataHolder.selectIcon = (ImageView) convertView.findViewById(R.id.select_imgview);
			dataHolder.selectIcon.setTag(false);
			convertView.setTag(dataHolder);
			clearView.add(convertView);
		} else {
			dataHolder = (DataHolder) convertView.getTag();
		}

		final File file = mFiles[position];
		mFinalBitmap.display(dataHolder.imgView, file.getAbsolutePath());
		dataHolder.imgAbsPath = file.getAbsolutePath();
		// 往下拖拽的时候，如果已经点击了全选/取消，那么需要立刻设置
		if (selectedAll) {
			dataHolder.selectIcon.setBackgroundResource(R.drawable.select_icon);
			dataHolder.selectIcon.setTag(true);
		} else {
			dataHolder.selectIcon.setBackgroundResource(R.drawable.unselect_icon);
			dataHolder.selectIcon.setTag(false);
		}
		dataHolder.imgView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(mContext, CurlActivity.class);
				Bundle extras = new Bundle();
				extras.putSerializable("files", mFiles);
				intent.putExtra("extractType", CurlActivity.EXTRACT_BY_URL);
				intent.putExtra(CurlActivity.EXTRACT_FOLDERNAME,file.getParentFile().getName());
				intent.putExtras(extras);
				intent.putExtra("currentIndex", position);
				Log.i("curl", "in currentIndex="+position);
				mContext.startActivity(intent);
			}
		});

		dataHolder.selectIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if ((Boolean) v.getTag()) {
					v.setBackgroundResource(R.drawable.unselect_icon);
					v.setTag(false);
					if (tempList.contains(file.getAbsolutePath())) {
						tempList.remove(file.getAbsolutePath());
					}
				} else {
					v.setBackgroundResource(R.drawable.select_icon);
					v.setTag(true);
					if (!tempList.contains(file.getAbsolutePath())) {
						tempList.add(file.getAbsolutePath());
					}
				}
			}
		});
		return convertView;

	}

	public List<String> getSelectedObject() {
		// Toast.makeText(mContext, "共选中:" + tempList.size() + "个对象",
		// Toast.LENGTH_SHORT).show();
		return this.tempList;
	}

	// -----------领域模型----------------//

	public class DataHolder {
		public ImageView imgView = null;
		public ImageView selectIcon = null;
		public TextView pkgNameView = null;
		public String imgAbsPath = null;
		public boolean selected = false;

	}

	@Override
	public void clearData() {
		//clear mFiles
	}

	@Override
	public void clearView() {
		for (View view : clearView) {
			if (view == null || view.getTag() == null) {
				continue;
			}
			DataHolder vh = (DataHolder) view.getTag();
			vh.imgView.setImageBitmap(null);
			vh.selectIcon.setImageBitmap(null);
			//vh.pkgNameView.setText(null);
		}
		clearView.clear();
	}
}
